package com.zzw.controller;

import com.zzw.dto.AssignAuthDto;
import com.zzw.entity.Result;
import com.zzw.entity.Role;
import com.zzw.page.Page;
import com.zzw.service.AuthService;
import com.zzw.service.RoleService;
import com.zzw.entity.CurrentUser;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/role")
@Slf4j
@Api(tags = "角色模块")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private AuthService authService;

    /**
     * 查询所有角色的url接口
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有角色")
    public Result findAllRole() {
        List<Role> roleList = roleService.findAllRole();
        return Result.ok(roleList);
    }

    /**
     * 分页查询角色的url接口
     *
     * @param page
     * @param role
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询角色")
    public Result roleListPage(Page page, Role role) {
        Page page1 = roleService.queryRolePage(page, role);
        return Result.ok(page1);
    }

    /**
     * 添加角色的url接口
     *
     * @param role
     * @param token
     * @return
     */
    @PostMapping
    @ApiOperation("添加角色")
    public Result saveRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录用户的id,即创建新角色的用户id
        int createBy = currentUser.getUserId();
        role.setCreateBy(createBy);

        return roleService.saveRole(role);
    }

    /**
     * 修改角色状态的url接口
     *
     * @param role
     * @param token
     * @return
     */
    @PutMapping("/state")
    @ApiOperation("修改角色状态")
    public Result updateRoleState(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即修改角色的用户id
        int updateBy = currentUser.getUserId();

        //设置修改角色的用户id和修改时间
        role.setUpdateBy(updateBy);
        role.setUpdateTime(new Date());

        //执行业务
        return roleService.updateRoleState(role);
    }

    @PutMapping
    @ApiOperation("更新角色")
    public Result updateRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id -- 修改角色的用户id
        int updateBy = currentUser.getUserId();

        role.setUpdateBy(updateBy);

        //执行业务
        return roleService.updateRoleDesc(role);
    }

    /**
     * 删除角色的url接口
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public Result deleteRole(@PathVariable Integer roleId) {
        return roleService.deleteRole(roleId);
    }

    /**
     * 给角色分配权限的url接口
     *
     * @param assignAuthDto
     * @return
     */
    @PutMapping("/auth")
    @ApiOperation("给角色分配权限")
    public Result assignAuth(@RequestBody AssignAuthDto assignAuthDto) {
        authService.assignAuth(assignAuthDto);
        return Result.ok("分配权限成功！");
    }

    /**
     * 查询角色已分配的权限(菜单)的url接口/role/role-auth
     * Integer roleId将请求参数roleId赋值给请求处理方法参数roleId;
     * 返回值Result对象向客户端响应组装了给角色分配的所有权限(菜单)id的List<Integer>;
     */
    @GetMapping("/role-auth/{roleId}")
    public Result queryRoleAuth(@PathVariable Integer roleId) {
        //执行业务
        List<Integer> authIdList = roleService.queryAuthIds(roleId);
        //响应
        return Result.ok(authIdList);
    }

    /**
     * 导出数据
     *
     * @param page
     * @param role
     * @return
     */
    @GetMapping("/exportTable")
    public Result exportTable(Page page, Role role) {
        page = roleService.queryRolePage(page, role);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }

}
